package com.config;

import com.generator.entity.Route;
import com.generator.mapper.RouteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;

/**
 * @Author: htj
 * @Date: 2021/10/4 0004 下午 15:54
 */
@Configuration
public class AddBean {

    @Autowired
    private RouteMapper routeMapper;

    //注入最大并发数map
    @Bean
    public HashMap<Integer, Semaphore> apiConcurrentMap() {

        //全局唯一的map，存储最高并发数。生成环境可以做成可配置项，通过注入方式进行注入！
        HashMap<Integer, Semaphore> apiConcurrentMap = new HashMap<>();

        List<Route> routeList = routeMapper.selectList(null);//查询网关中存在的所有接口

        for (int i = 0; i < routeList.size(); i++) {
            Semaphore semaphore = new Semaphore(routeList.get(i).getConcurrent());
            apiConcurrentMap.put(routeList.get(i).getId(), semaphore);//key是接口id，value是接口的最大并发数
        }

        //遍历map并打印
        Iterator iter = apiConcurrentMap.entrySet().iterator();
        int key;
        Semaphore value;
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            key = (Integer) entry.getKey();
            value = (Semaphore) entry.getValue();
            System.out.println("接口号:" + key + "    单机最高并发数:" + value);
        }

        return apiConcurrentMap;
    }

    //注入超时时间map
    @Bean
    public HashMap<Integer, Integer> apiOvertimeMap() {

        //全局唯一的map，存储超时时间。生成环境可以做成可配置项，通过注入方式进行注入！
        HashMap<Integer, Integer> apiOvertimeMap = new HashMap<>();

        List<Route> routeList = routeMapper.selectList(null);//查询网关中存在的所有接口

        for (int i = 0; i < routeList.size(); i++) {
            int overtime = routeList.get(i).getOvertime();
            apiOvertimeMap.put(routeList.get(i).getId(), overtime);//key是接口id，value是接口的超时时间
        }

        //遍历map并打印
        Iterator iter = apiOvertimeMap.entrySet().iterator();
        int key;
        int value;
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            key = (Integer) entry.getKey();
            value = (Integer) entry.getValue();
            System.out.println("接口号:" + key + "    超时时间:" + value);
        }

        return apiOvertimeMap;
    }

    //注入开关map
    @Bean
    public HashMap<Integer, Integer> apiSwitchMap() {

        //全局唯一的map，存储超时时间
        HashMap<Integer, Integer> apiSwitchMap = new HashMap<>();

        List<Route> routeList = routeMapper.selectList(null);//查询网关中存在的所有接口

        for (int i = 0; i < routeList.size(); i++) {
            int available = routeList.get(i).getAvailable();
            apiSwitchMap.put(routeList.get(i).getId(), available);//key是接口id，value是接口的开关
        }

        //遍历map并打印
        Iterator iter = apiSwitchMap.entrySet().iterator();
        int key;
        int value;
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            key = (Integer) entry.getKey();
            value = (Integer) entry.getValue();
            System.out.println("接口号:" + key + "    开关:" + value);
        }

        return apiSwitchMap;
    }


}
